home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / btree / bt_get.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  6.5 KB  |  243 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Mike Olson.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)bt_get.c    8.2 (Berkeley) 9/7/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. #include <sys/types.h>
  46.  
  47. #include <errno.h>
  48. #include <stddef.h>
  49. #include <stdio.h>
  50.  
  51. #include <db.h>
  52. #include "btree.h"
  53.  
  54. /*
  55.  * __BT_GET -- Get a record from the btree.
  56.  *
  57.  * Parameters:
  58.  *    dbp:    pointer to access method
  59.  *    key:    key to find
  60.  *    data:    data to return
  61.  *    flag:    currently unused
  62.  *
  63.  * Returns:
  64.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  65.  */
  66. int
  67. __bt_get(dbp, key, data, flags)
  68.     const DB *dbp;
  69.     const DBT *key;
  70.     DBT *data;
  71.     u_int flags;
  72. {
  73.     BTREE *t;
  74.     EPG *e;
  75.     int exact, status;
  76.  
  77.     t = dbp->internal;
  78.  
  79.     /* Toss any page pinned across calls. */
  80.     if (t->bt_pinned != NULL) {
  81.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  82.         t->bt_pinned = NULL;
  83.     }
  84.  
  85.     /* Get currently doesn't take any flags. */
  86.     if (flags) {
  87.         errno = EINVAL;
  88.         return (RET_ERROR);
  89.     }
  90.  
  91.     if ((e = __bt_search(t, key, &exact)) == NULL)
  92.         return (RET_ERROR);
  93.     if (!exact) {
  94.         mpool_put(t->bt_mp, e->page, 0);
  95.         return (RET_SPECIAL);
  96.     }
  97.  
  98.     /*
  99.      * A special case is if we found the record but it's flagged for
  100.      * deletion.  In this case, we want to find another record with the
  101.      * same key, if it exists.  Rather than look around the tree we call
  102.      * __bt_first and have it redo the search, as __bt_first will not
  103.      * return keys marked for deletion.  Slow, but should never happen.
  104.      */
  105.     if (ISSET(t, B_DELCRSR) && e->page->pgno == t->bt_bcursor.pgno &&
  106.         e->index == t->bt_bcursor.index) {
  107.         mpool_put(t->bt_mp, e->page, 0);
  108.         if ((e = __bt_first(t, key, &exact)) == NULL)
  109.             return (RET_ERROR);
  110.         if (!exact)
  111.             return (RET_SPECIAL);
  112.     }
  113.  
  114.     status = __bt_ret(t, e, NULL, data);
  115.     /*
  116.      * If the user is doing concurrent access, we copied the
  117.      * key/data, toss the page.
  118.      */
  119.     if (ISSET(t, B_DB_LOCK))
  120.           mpool_put(t->bt_mp, e->page, 0);
  121.     else
  122.         t->bt_pinned = e->page;
  123.     return (status);
  124. }
  125.  
  126. /*
  127.  * __BT_FIRST -- Find the first entry.
  128.  *
  129.  * Parameters:
  130.  *    t:    the tree
  131.  *    key:    the key
  132.  *
  133.  * Returns:
  134.  *    The first entry in the tree greater than or equal to key.
  135.  */
  136. EPG *
  137. __bt_first(t, key, exactp)
  138.     BTREE *t;
  139.     const DBT *key;
  140.     int *exactp;
  141. {
  142.     register PAGE *h;
  143.     register EPG *e;
  144.     EPG save;
  145.     pgno_t cpgno, pg;
  146.     indx_t cindex;
  147.     int found;
  148.  
  149.     /*
  150.      * Find any matching record; __bt_search pins the page.  Only exact
  151.      * matches are tricky, otherwise just return the location of the key
  152.      * if it were to be inserted into the tree.
  153.      */
  154.     if ((e = __bt_search(t, key, exactp)) == NULL)
  155.         return (NULL);
  156.     if (!*exactp)
  157.         return (e);
  158.  
  159.     if (ISSET(t, B_DELCRSR)) {
  160.         cpgno = t->bt_bcursor.pgno;
  161.         cindex = t->bt_bcursor.index;
  162.     } else {
  163.         cpgno = P_INVALID;
  164.         cindex = 0;        /* GCC thinks it's uninitialized. */
  165.     }
  166.  
  167.     /*
  168.      * Walk backwards, skipping empty pages, as long as the entry matches
  169.      * and there are keys left in the tree.  Save a copy of each match in
  170.      * case we go too far.  A special case is that we don't return a match
  171.      * on records that the cursor references that have already been flagged
  172.      * for deletion.
  173.      */
  174.     save = *e;
  175.     h = e->page;
  176.     found = 0;
  177.     do {
  178.         if (cpgno != h->pgno || cindex != e->index) {
  179.             if (save.page->pgno != e->page->pgno) {
  180.                 mpool_put(t->bt_mp, save.page, 0);
  181.                 save = *e;
  182.             } else
  183.                 save.index = e->index;
  184.             found = 1;
  185.         }
  186.         /*
  187.          * Make a special effort not to unpin the page the last (or
  188.          * original) match was on, but also make sure it's unpinned
  189.          * if an error occurs.
  190.          */
  191.         while (e->index == 0) {
  192.             if (h->prevpg == P_INVALID)
  193.                 goto done1;
  194.             if (h->pgno != save.page->pgno)
  195.                 mpool_put(t->bt_mp, h, 0);
  196.             if ((h = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) {
  197.                 if (h->pgno == save.page->pgno)
  198.                     mpool_put(t->bt_mp, save.page, 0);
  199.                 return (NULL);
  200.             }
  201.             e->page = h;
  202.             e->index = NEXTINDEX(h);
  203.         }
  204.         --e->index;
  205.     } while (__bt_cmp(t, key, e) == 0);
  206.  
  207.     /*
  208.      * Reach here with the last page that was looked at pinned, which may
  209.      * or may not be the same as the last (or original) match page.  If
  210.      * it's not useful, release it.
  211.      */
  212. done1:    if (h->pgno != save.page->pgno)
  213.         mpool_put(t->bt_mp, h, 0);
  214.  
  215.     /*
  216.      * If still haven't found a record, the only possibility left is the
  217.      * next one.  Move forward one slot, skipping empty pages and check.
  218.      */
  219.     if (!found) {
  220.         h = save.page;
  221.         if (++save.index == NEXTINDEX(h)) {
  222.             do {
  223.                 pg = h->nextpg;
  224.                 mpool_put(t->bt_mp, h, 0);
  225.                 if (pg == P_INVALID) {
  226.                     *exactp = 0;
  227.                     return (e);
  228.                 }
  229.                 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  230.                     return (NULL);
  231.             } while ((save.index = NEXTINDEX(h)) == 0);
  232.             save.page = h;
  233.         }
  234.         if (__bt_cmp(t, key, &save) != 0) {
  235.             *exactp = 0;
  236.             return (e);
  237.         }
  238.     }
  239.     *e = save;
  240.     *exactp = 1;
  241.     return (e);
  242. }
  243.